#import "EditableMovie.h"

@implementation EditableMovie

//////////
//
// init
//
// initialize our method variables
//
//////////

- (id)init
{
    self = [super init];

    if (self) 
	{
		mMovie			= nil;
		mDataHandlerRef = nil;
    }
	[self buildQTKitMovie];
    return self;
}

//////////
//
// dealloc
//
// do cleanup here
//
//////////

- (void)dealloc
{
	[mMovie release];

	if (mDataHandlerRef)
		CloseMovieStorage(mDataHandlerRef);

	[super dealloc];
}

//////////
//
// movie
//
// getter for mMovie QTMovie class variable
//
//////////

-(QTMovie *)movie
{
	return mMovie;
}


//
// quicktimeMovieFromTempFile
//
// Creates a QuickTime movie file from a temporary file
//
//

-(Movie)quicktimeMovieFromTempFile:(DataHandler *)outDataHandler error:(OSErr *)outErr
{
	*outErr = -1;
	
	// generate a name for our movie file
	NSString *tempName = [NSString stringWithCString:tmpnam(nil) 
							encoding:[NSString defaultCStringEncoding]];
	if (nil == tempName) goto nostring;
	
	Handle	dataRefH		= nil;
	OSType	dataRefType;

	// create a file data reference for our movie
	*outErr = QTNewDataReferenceFromFullPathCFString((CFStringRef)tempName,
												  kQTNativeDefaultPathStyle,
												  0,
												  &dataRefH,
												  &dataRefType);
	if (*outErr != noErr) goto nodataref;
	
	// create a QuickTime movie from our file data reference
	Movie	qtMovie	= nil;
	CreateMovieStorage (dataRefH,
						dataRefType,
						'TVOD',
						smSystemScript,
						newMovieActive, 
						outDataHandler,
						&qtMovie);
	*outErr = GetMoviesError();
	if (*outErr != noErr) goto cantcreatemovstorage;

	return qtMovie;

// error handling
cantcreatemovstorage:
	DisposeHandle(dataRefH);
nodataref:
nostring:

	return nil;
}


//
// buildQTKitMovie
//
// Build a QTKit movie from a series of image frames
//
//

-(void)buildQTKitMovie
{

/*  

	NOTES ABOUT CREATING A NEW ("EMPTY") MOVIE AND ADDING IMAGE FRAMES TO IT

	In order to compose a new movie from a series of image frames with QTKit
	it is of course necessary to first create an "empty" movie to which these
	frames can be added. Actually, the real requirements (in QuickTime terminology)
	for such an "empty" movie are that it contain a writable data reference. A
	movie with a writable data reference can then accept the addition of image 
	frames via the -addImage method.
	
	A future version of QTKit will provide a simple Obj C method for creating 
	a QTMovie with a writable data reference.

	In the meantime, we can use the native QuickTime API CreateMovieStorage to create
	a QuickTime movie with a writable data reference (in our example below we use a
	data reference to a file). We then use the QTKit movieWithQuickTimeMovie: method to
	instantiate a QTMovie from this QuickTime movie. 
	
	Finally, images are added to the movie as movie frames using -addImage.

*/

	OSErr err;
	// create a QuickTime movie
	Movie qtMovie = [self quicktimeMovieFromTempFile:&mDataHandlerRef error:&err];
	if (nil == qtMovie) goto bail;
	
	// instantiate a QTMovie from our QuickTime movie
	mMovie = [QTMovie movieWithQuickTimeMovie:qtMovie disposeWhenDone:YES error:nil];
	if (!mMovie || err) goto bail;
	
	// mark the movie as editable
	[mMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
	
	// keep it around until we are done with it...
	[mMovie retain];


bail:

	return;
}


// Facade methods -- pass through to mMovie

- (void)addImage:(NSImage *)image forDuration:(QTTime)duration withAttributes:(NSDictionary *)attributes{
    [mMovie addImage: image forDuration: duration withAttributes: attributes];
}

- (NSImage *)currentFrameImage{
    return [mMovie currentFrameImage];
}

- (QTTime)duration{
    return [mMovie duration];
}

- (NSImage *)frameImageAtTime:(QTTime)time{
    return [mMovie frameImageAtTime: time];
}

-(BOOL)writeToFile:(NSString *)fileName withAttributes:(NSDictionary *)attributes{
    return [mMovie writeToFile: fileName withAttributes: attributes];
}


@end

